package com.voxel.sense.infomsg.widget.map.adpter;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.data.bean.InfoWindowSiteItemBean;
import com.voxel.sense.infomsg.data.bean.MultiItemBean;
import com.voxel.sense.infomsg.data.bean.RealDataBean;
import com.voxel.sense.infomsg.view.progress.ScaleProgressView;

import java.util.Random;

import cyy.example.com.facotry.constants.Constants;

import butterknife.BindView;
import cyy.example.com.facotry.model.api.response.FirmLatestOverDataEntity;

/**
 * Created by ChenYiYao on 2018/8/28.
 */
public class CustomInfoRecyclerAdapter extends RecyclerAdapter<MultiItemBean> {

    @Override
    protected int getItemViewType(int position, MultiItemBean multiItemBean) {
        int type = multiItemBean.getType();
        int layout = 0;
        if (type == Constants.DES_TYPE) {
            layout = R.layout.item_recycler_map_marker_info_des;
        } else if (type == Constants.FACILITY_TYPE) {
            layout = R.layout.item_recycler_map_marker_info_facility;
        } else {
            layout = R.layout.item_recycler_map_marker_info_element;

        }
        return layout;
    }

    @Override
    protected RecyclerAdapter.ViewHolder<MultiItemBean> onCreateViewHolder(View root, int viewType) {
        if (viewType == R.layout.item_recycler_map_marker_info_des) {
            return new DesViewHolder(root);
        } else if (viewType == R.layout.item_recycler_map_marker_info_facility) {
            return new FacilityViewHolder(root);
        } else {
            return new ViewHolder(root);
        }
    }


    static class DesViewHolder extends RecyclerAdapter.ViewHolder<MultiItemBean> {
        @BindView(R.id.tv_time)
        TextView tv_time;
        @BindView(R.id.tv_id)
        TextView tv_id;
        @BindView(R.id.tv_style)
        TextView tv_style;

        public DesViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(MultiItemBean data) {
            InfoWindowSiteItemBean bean = (InfoWindowSiteItemBean) data.getData();
//
            tv_time.setText(bean.getDatetime());
            tv_id.setText(bean.getWatchsiteName());
            String type = bean.getType();
            if (TextUtils.isEmpty(type)) {
                tv_style.setVisibility(View.INVISIBLE);
            } else {
                tv_style.setVisibility(View.VISIBLE);
                tv_style.setText(type);
            }

        }
    }

    /**
     * 设备异常hoder
     */
    static class FacilityViewHolder extends RecyclerAdapter.ViewHolder<MultiItemBean> {
//        @BindView(R.id.progress_bar)
//        ScaleProgressView progress_bar;
        @BindView(R.id.progress_bar)
        ProgressBar progress_bar;
        @BindView(R.id.tv_progres)
        TextView tv_progres;

        public FacilityViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(MultiItemBean multiItemBean) {
            String progresStr = (String) multiItemBean.getData();
            try {
                //去除小数点及后面的数
                String s = progresStr.contains(".") ? progresStr.substring(0, progresStr.indexOf(".")) : progresStr;
                int value = Integer.parseInt(s);
                progress_bar.setProgress(value);
                tv_progres.setText(progresStr + "%");
            } catch (NumberFormatException e) {
                progress_bar.setProgress(0);
                tv_progres.setText("0%");
                e.printStackTrace();
            }
        }
    }

    static class ViewHolder extends RecyclerAdapter.ViewHolder<MultiItemBean> {
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.tv_value)
        TextView tv_value;
        @BindView(R.id.tv_standard_value)
        TextView tv_standard_value;
        @BindView(R.id.tv_name_two)
        TextView tv_name_two;
        @BindView(R.id.tv_value_two)
        TextView tv_value_two;
        @BindView(R.id.tv_standard_value_two)
        TextView tv_standard_value_two;

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(MultiItemBean multiItemBean) {
            RealDataBean[] beans = (RealDataBean[]) multiItemBean.getData();
            RealDataBean dataBean1 = beans[0];
            if (dataBean1 != null) {
                bindData(tv_name, tv_value, tv_standard_value, dataBean1);
            }

            RealDataBean dataBean2 = beans[1];
            if (dataBean2 != null && !TextUtils.isEmpty(dataBean2.getParamName())) {
                bindData(tv_name_two, tv_value_two, tv_standard_value_two, dataBean2);
                tv_name_two.setVisibility(View.VISIBLE);
                tv_value_two.setVisibility(View.VISIBLE);
                tv_standard_value_two.setVisibility(View.VISIBLE);
            } else {
                tv_name_two.setVisibility(View.INVISIBLE);
                tv_value_two.setVisibility(View.INVISIBLE);
                tv_standard_value_two.setVisibility(View.INVISIBLE);
            }
        }

        private void bindData(TextView name, TextView value, TextView standard, RealDataBean dataBean1) {
            name.setText(dataBean1.getParamName());
            value.setText(dataBean1.getValue());

            String paramUnit = dataBean1.getParamUnit();
            String standarValues = dataBean1.getStandarValues();
            String sValue = "";

            if (!TextUtils.isEmpty(standarValues) && !TextUtils.isEmpty(paramUnit)) {
                sValue = "/" + (dataBean1.getStandarValues() + "(" + paramUnit + ")");
            } else if (!TextUtils.isEmpty(standarValues)) {
                //标准值不为空
                sValue = "/" + dataBean1.getStandarValues();
            } else if (!TextUtils.isEmpty(paramUnit)) {
                //单位不为空
                sValue = "(" + paramUnit + ")";
            }
            standard.setText(sValue);
        }
    }

}
